package cn.net.isir.core;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class RequestHolder {
	public static HttpServletRequest getRequest() {
		HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return req;
	}

	public static HttpServletResponse getResponse() {
		HttpServletResponse resp = ((ServletWebRequest) RequestContextHolder.getRequestAttributes()).getResponse();
		return resp;
	}

	public static HttpSession getSession() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession(true);
	}

	public static HttpSession getSession(boolean create) {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession(create);
	}

	/**
	 * 获取request请求参数
	 */
	public static String getAttribute(String param) {
		String str = getRequest().getParameter(param);
		if (str == null || str.equals("null")) {
			return null;
		}
		return str;
	}

	/**
	 * 设置request参数值
	 */
	public static void setAttribute(String key, Object value) {
		getRequest().setAttribute(key, value);
	}

	/**
	 * 获取session值
	 */
	public static Object getSessionAttribute(String param) {
		Object str = getSession().getAttribute(param);
		if (str == null || str.equals("null")) {
			return null;
		}
		return str;
	}

	/**
	 * 赋值Session
	 */
	public static void setSessionAttribute(String key, Object value) {
		getSession().setAttribute(key, value);
	}

	/**
	 * 移除session值
	 */
	public static void removeSessionAttribute(String key) {
		getSession().removeAttribute(key);
	}
}
